home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 26 / CU Amiga Magazine's Super CD-ROM 26 (1998)(EMAP Images)(GB)[!][issue 1998-09].iso / CUCD / PowerPC / uae-0.8.4 / amiga / source / uae-control.c < prev    next >
C/C++ Source or Header  |  1998-03-15  |  15KB  |  564 lines

  1. /***********************************************************
  2. * UAE - The U*nix Amiga Emulator                           *
  3. *                                                          *
  4. * UAE-Control - Emulator control from inside emulation     *
  5. *  (c) 1996 Tauno Taipaleenmaki <tataipal@raita.oulu.fi>   *
  6. *  (c) 1998 Cloanto <info@cloanto.com>                     *
  7. *                                                          *
  8. * Version 1.1                                              *
  9. *                                                          *
  10. * Requires Amiga OS v. 36 or higher. Compiled with SAS/C   *
  11. *                                                          *
  12. * Users with KS 1.3 or below should use the command line   *
  13. * version "uaectrl"                                        *
  14. ***********************************************************/
  15.  
  16. #define VERSION_STRING "\0$VER: UAE-Control 1.1"
  17. #define VERSION_SHELL " UAE-Control v. 1.1\n © 1996 Tauno Taipaleenmaki, © 1998 Cloanto\n\n"
  18.  
  19. #include <intuition/intuition.h>
  20. #ifndef _DCC
  21. #include <proto/exec.h>
  22. #include <proto/intuition.h>
  23. #include <proto/gadtools.h>
  24. #include <proto/graphics.h>
  25. #include <proto/dos.h>
  26. #else
  27. #include <clib/exec_protos.h>
  28. #include <clib/intuition_protos.h>
  29. #include <clib/gadtools_protos.h>
  30. #include <clib/graphics_protos.h>
  31. #include <clib/dos_protos.h>
  32. #endif
  33. #include <stdio.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include "UAE-Control.h"
  37. #include "uae_pragmas.h"
  38.  
  39. #define MAX_DRV_NAME  25
  40.  
  41. struct Window *window;
  42. struct Screen *scr;
  43. struct Gadget *glist;
  44. APTR VisInfo;
  45. struct UAE_CONFIG config;
  46.  
  47. UBYTE *version_string = VERSION_STRING;
  48.  
  49. struct TextAttr topaz8 =
  50. {
  51.     (STRPTR) "topaz.font", 8, 0x00, 0x01
  52. };
  53.  
  54. struct NewWindow newwindow =
  55. {
  56.     0, 0, 0, 0, 0, 1,
  57. IDCMP_CLOSEWINDOW | BUTTONIDCMP | CYCLEIDCMP | INTEGERIDCMP | STRINGIDCMP,
  58.     WFLG_DRAGBAR | WFLG_CLOSEGADGET | WFLG_ACTIVATE | WFLG_DEPTHGADGET,
  59.     NULL, NULL, NULL, NULL, NULL,
  60.     0, 0, 0, 0, CUSTOMSCREEN
  61. };
  62.  
  63. int setup_window(void);
  64. void quit_program(int error, char *text);
  65. void print_drive_status(void);
  66. BOOL get_string(UBYTE * buff, LONG max_len);
  67.  
  68.  
  69. /*****************************************
  70. * Main program                           *
  71. *****************************************/
  72. int main()
  73. {
  74.     int quit = 0, i;
  75.     struct IntuiMessage *msg;
  76.     struct Gadget *button;
  77.     struct StringInfo *strinfo;
  78.     char buf[257];
  79.     UWORD koodi, msgID;
  80.     ULONG classi, number;
  81.     APTR address;
  82.     ULONG CycleTags[3];
  83.  
  84.     CycleTags[0] = GTCY_Active;
  85.     CycleTags[1] = (ULONG) & number;
  86.     CycleTags[2] = TAG_DONE;
  87.  
  88.     /* Read UAE configuration */
  89.     i = GetUaeConfig(&config);
  90.     i = setup_window();
  91.     if (i == 0) {
  92.     quit_program(1, "Cannot setup a window!");
  93.     return 1;
  94.     }
  95.     while (quit == 0) {
  96.     WaitPort(window->UserPort);
  97.     while (msg = (struct IntuiMessage *) GT_GetIMsg(window->UserPort)) {
  98.         classi = msg->Class;
  99.         koodi = msg->Code;
  100.         address = msg->IAddress;
  101.         if (classi == IDCMP_GADGETUP) {
  102.         msgID = ((struct Gadget *) msg->IAddress)->GadgetID;
  103.         button = (struct Gadget *) msg->IAddress;
  104.         if (button->SpecialInfo) {
  105.             strinfo = (struct StringInfo *) button->SpecialInfo;
  106.         }
  107.         } else
  108.         msgID = msg->Code;
  109.  
  110.         GT_ReplyIMsg((struct IntuiMessage *) msg);
  111.         switch (classi) {
  112.         case IDCMP_CLOSEWINDOW:
  113.         quit = 1;
  114.         break;
  115.         case IDCMP_GADGETUP:
  116.         switch (msgID) {
  117.         case GAD_EXITEMU:
  118.             ExitEmu();
  119.             break;
  120.         case GAD_EJECT_DF0:
  121.             EjectDisk(0);
  122.             Delay(30);
  123.             GetUaeConfig(&config);
  124.             print_drive_status();
  125.             break;
  126.         case GAD_EJECT_DF1:
  127.             EjectDisk(1);
  128.             Delay(30);
  129.             GetUaeConfig(&config);
  130.             print_drive_status();
  131.             break;
  132.         case GAD_EJECT_DF2:
  133.             EjectDisk(2);
  134.             Delay(30);
  135.             GetUaeConfig(&config);
  136.             print_drive_status();
  137.             break;
  138.         case GAD_EJECT_DF3:
  139.             EjectDisk(3);
  140.             Delay(30);
  141.             GetUaeConfig(&config);
  142.             print_drive_status();
  143.             break;
  144.         case GAD_SOUND:
  145.             if (config.do_output_sound)
  146.             DisableSound();
  147.             else
  148.             EnableSound();
  149.  
  150.             Delay(30);
  151.             GetUaeConfig(&config);
  152.             break;
  153.         case GAD_JOYSTICK:
  154.             if (config.do_fake_joystick)
  155.             DisableJoystick();
  156.             else
  157.             EnableJoystick();
  158.  
  159.             Delay(30);
  160.             GetUaeConfig(&config);
  161.             break;
  162.         case GAD_FRAMERATE:
  163.             SetFrameRate(strinfo->LongInt);
  164.             Delay(30);
  165.             GetUaeConfig(&config);
  166.             break;
  167.         case GAD_INSERT_DF0:
  168.             strcpy(buf, config.df0_name);
  169.             if (get_string(buf, 255)) {
  170.             InsertDisk((UBYTE *) buf, 0);
  171.             Delay(30);
  172.             GetUaeConfig(&config);
  173.             print_drive_status();
  174.             }
  175.             break;
  176.         case GAD_INSERT_DF1:
  177.             strcpy(buf, config.df1_name);
  178.             if (get_string(buf, 255)) {
  179.             InsertDisk((UBYTE *) buf, 1);
  180.             Delay(30);
  181.             GetUaeConfig(&config);
  182.             print_drive_status();
  183.             }
  184.             break;
  185.         case GAD_INSERT_DF2:
  186.             strcpy(buf, config.df2_name);
  187.             if (get_string(buf, 255)) {
  188.             InsertDisk((UBYTE *) buf, 2);
  189.             Delay(30);
  190.             GetUaeConfig(&config);
  191.             print_drive_status();
  192.             }
  193.             break;
  194.         case GAD_INSERT_DF3:
  195.             strcpy(buf, config.df3_name);
  196.             if (get_string(buf, 255)) {
  197.             InsertDisk((UBYTE *) buf, 3);
  198.             Delay(30);
  199.             GetUaeConfig(&config);
  200.             print_drive_status();
  201.             }
  202.             break;
  203.         case GAD_LANGUAGE:
  204.             number = config.keyboard;
  205.             number++;
  206.             if (number == 5)
  207.             number = 0;
  208.             ChangeLanguage(number);
  209.             Delay(30);
  210.             GetUaeConfig(&config);
  211.             break;
  212.         case GAD_RESET:
  213.             ColdReboot();
  214.             break;
  215.         case GAD_DEBUG:
  216.             DebugFunc();
  217.             break;
  218.         default:
  219.             break;
  220.         }
  221.         break;
  222.         default:
  223.         break;
  224.         }
  225.     }
  226.     }
  227.     quit_program(0, "");
  228.     return (0);
  229. }
  230.  
  231. /*****************************************
  232. * Quits the program                      *
  233. *****************************************/
  234. void quit_program(int error, char *text)
  235. {
  236.     if (error > 0) {
  237.     printf(VERSION_SHELL);
  238.     printf(" ERROR: %s\n", text);
  239.     }
  240.     if (scr)
  241.     UnlockPubScreen(NULL, scr);
  242.     if (window)
  243.     CloseWindow(window);
  244.     if (VisInfo)
  245.     FreeVisualInfo(VisInfo);
  246.     if (glist)
  247.     FreeGadgets(glist);
  248. }
  249.  
  250. /*****************************************
  251. * Opens up the window & sets the gadgets *
  252. *****************************************/
  253. int setup_window(void)
  254. {
  255.     struct Gadget *g;
  256.     struct NewGadget ng;
  257.     UWORD offy;
  258.     ULONG drawtags[20];
  259.     static const char *keyb_langs[] =
  260.     {
  261.     "US",
  262.     "DE",
  263.     "SE",
  264.     "FR",
  265.     "IT",
  266.     NULL
  267.     };
  268.     scr = LockPubScreen(NULL);
  269.     if (!scr)
  270.     return (0);
  271.     VisInfo = GetVisualInfo(scr, TAG_DONE);
  272.     if (!VisInfo)
  273.     return (0);
  274.     offy = scr->WBorTop + scr->RastPort.TxHeight + 1;
  275.  
  276.     /* Create buttons */
  277.     g = CreateContext(&glist);
  278.     if (!g)
  279.     return (0);
  280.  
  281.     ng.ng_TextAttr = &topaz8;
  282.     ng.ng_VisualInfo = VisInfo;
  283.     ng.ng_Flags = PLACETEXT_IN;
  284.     ng.ng_LeftEdge = 284;
  285.     ng.ng_TopEdge = offy + 86;
  286.     ng.ng_Width = 96;
  287.     ng.ng_Height = 15;
  288.     ng.ng_GadgetText = (UBYTE *) "Hard Reset";
  289.     ng.ng_GadgetID = GAD_RESET;
  290.     g = CreateGadget(BUTTON_KIND, g, &ng, TAG_DONE);
  291.  
  292.     ng.ng_TopEdge = offy + 105;
  293.     ng.ng_GadgetText = (UBYTE *) "Debug";
  294.     ng.ng_GadgetID = GAD_DEBUG;
  295.     g = CreateGadget(BUTTON_KIND, g, &ng, TAG_DONE);
  296.  
  297.     ng.ng_TopEdge = offy + 135;
  298.     ng.ng_GadgetText = (UBYTE *) "Quit UAE";
  299.     ng.ng_GadgetID = GAD_EXITEMU;
  300.     g = CreateGadget(BUTTON_KIND, g, &ng, TAG_DONE);
  301.  
  302.     /* Eject buttons */
  303.     ng.ng_LeftEdge = 262;
  304.     ng.ng_TopEdge = offy + 11;
  305.     ng.ng_Width = 70;
  306.     ng.ng_GadgetID = GAD_EJECT_DF0;
  307.     ng.ng_GadgetText = (UBYTE *) "Eject";
  308.     g = CreateGadget(BUTTON_KIND, g, &ng, TAG_DONE);
  309.  
  310.     ng.ng_TopEdge = offy + 28;
  311.     ng.ng_GadgetID = GAD_EJECT_DF1;
  312.     g = CreateGadget(BUTTON_KIND, g, &ng, TAG_DONE);
  313.  
  314.     ng.ng_TopEdge = offy + 45;
  315.     ng.ng_GadgetID = GAD_EJECT_DF2;
  316.     g = CreateGadget(BUTTON_KIND, g, &ng, TAG_DONE);
  317.  
  318.     ng.ng_TopEdge = offy + 62;
  319.     ng.ng_GadgetID = GAD_EJECT_DF3;
  320.     g = CreateGadget(BUTTON_KIND, g, &ng, TAG_DONE);
  321.  
  322.     /* Insert buttons */
  323.     ng.ng_LeftEdge = 332;
  324.     ng.ng_TopEdge = offy + 11;
  325.     ng.ng_GadgetID = GAD_INSERT_DF0;
  326.     ng.ng_GadgetText = (UBYTE *) "Insert";
  327.     g = CreateGadget(BUTTON_KIND, g, &ng, TAG_DONE);
  328.  
  329.     ng.ng_TopEdge = offy + 28;
  330.     ng.ng_GadgetID = GAD_INSERT_DF1;
  331.     g = CreateGadget(BUTTON_KIND, g, &ng, TAG_DONE);
  332.  
  333.     ng.ng_TopEdge = offy + 45;
  334.     ng.ng_GadgetID = GAD_INSERT_DF2;
  335.     g = CreateGadget(BUTTON_KIND, g, &ng, TAG_DONE);
  336.  
  337.     ng.ng_TopEdge = offy + 62;
  338.     ng.ng_GadgetID = GAD_INSERT_DF3;
  339.     g = CreateGadget(BUTTON_KIND, g, &ng, TAG_DONE);
  340.  
  341.     /* Sound & Joystick buttons */
  342.     ng.ng_LeftEdge = 133;
  343.     ng.ng_TopEdge = 97;
  344.     ng.ng_GadgetID = GAD_SOUND;
  345.     ng.ng_GadgetText = (UBYTE *) "Sound:";
  346.     ng.ng_Flags = PLACETEXT_LEFT;
  347.     g = CreateGadget(CHECKBOX_KIND, g, &ng, GTCB_Checked, config.do_output_sound ? 1 : 0, TAG_DONE);
  348.  
  349.     ng.ng_TopEdge = 114;
  350.     ng.ng_GadgetID = GAD_JOYSTICK;
  351.     ng.ng_GadgetText = (UBYTE *) "Fake Joystick:";
  352.     g = CreateGadget(CHECKBOX_KIND, g, &ng, GTCB_Checked, config.do_fake_joystick ? 1 : 0, TAG_DONE);
  353.  
  354.     ng.ng_TopEdge = 129;
  355.     ng.ng_GadgetID = GAD_LANGUAGE;
  356.     ng.ng_GadgetText = (UBYTE *) "Language:";
  357.     g = CreateGadget(CYCLE_KIND, g, &ng,
  358.              GTCY_Labels, (ULONG) keyb_langs,
  359.              GTCY_Active, config.keyboard,
  360.              TAG_DONE);
  361.  
  362.     ng.ng_TopEdge = 146;
  363.     ng.ng_GadgetID = GAD_FRAMERATE;
  364.     ng.ng_GadgetText = (UBYTE *) "Framerate:";
  365.     g = CreateGadget(INTEGER_KIND, g, &ng, GTIN_Number, config.framerate, TAG_DONE);
  366.  
  367.     newwindow.Title = "UAE-Control";
  368.     newwindow.Width = 419;
  369.     newwindow.Height = 171;
  370.     newwindow.LeftEdge = 20;
  371.     newwindow.TopEdge = 20;
  372.     newwindow.FirstGadget = glist;
  373.     newwindow.Screen = scr;    /* Store screen ptr */
  374.     window = OpenWindow(&newwindow);
  375.     if (!window)
  376.     return (0);
  377.  
  378.     /* Draw texts etc... */
  379.     SetAPen(window->RPort, 1);
  380.     SetDrMd(window->RPort, JAM2);
  381.     Move(window->RPort, 19, offy + 11 + 10);
  382.     Text(window->RPort, (UBYTE *) "DF0:", 4);
  383.     Move(window->RPort, 19, offy + 28 + 10);
  384.     Text(window->RPort, (UBYTE *) "DF1:", 4);
  385.     Move(window->RPort, 19, offy + 45 + 10);
  386.     Text(window->RPort, (UBYTE *) "DF2:", 4);
  387.     Move(window->RPort, 19, offy + 62 + 10);
  388.     Text(window->RPort, (UBYTE *) "DF3:", 4);
  389.  
  390.     drawtags[0] = GT_VisualInfo;
  391.     drawtags[1] = (ULONG) VisInfo;
  392.     drawtags[2] = GTBB_Recessed;
  393.     drawtags[3] = 1;
  394.     drawtags[4] = TAG_DONE;
  395.     DrawBevelBoxA(window->RPort, 14, offy + 10, 390, 17, (struct TagItem *) &drawtags);
  396.     DrawBevelBoxA(window->RPort, 14, offy + 27, 390, 17, (struct TagItem *) &drawtags);
  397.     DrawBevelBoxA(window->RPort, 14, offy + 44, 390, 17, (struct TagItem *) &drawtags);
  398.     DrawBevelBoxA(window->RPort, 14, offy + 61, 390, 17, (struct TagItem *) &drawtags);
  399.  
  400.     print_drive_status();
  401.  
  402.     return (1);
  403. }
  404.  
  405. void print_drive_status(void)
  406. {
  407.     char empty[80];
  408.     int len;
  409.     UWORD offy = scr->WBorTop + scr->RastPort.TxHeight + 1;
  410.  
  411.     for (len = 0; len < 80; len++)
  412.     empty[len] = ' ';
  413.     empty[79] = '\0';
  414.  
  415.     SetAPen(window->RPort, 1);
  416.     SetDrMd(window->RPort, JAM2);
  417.  
  418.     Move(window->RPort, 59, offy + 11 + 10);
  419.     if (config.disk_in_df0) {
  420.     len = strlen(config.df0_name);
  421.     if (len > MAX_DRV_NAME)
  422.         len = MAX_DRV_NAME;
  423.     Text(window->RPort, (UBYTE *) config.df0_name, len);
  424.     } else {
  425.     Text(window->RPort, (UBYTE *) empty, MAX_DRV_NAME);
  426.     }
  427.     Move(window->RPort, 59, offy + 28 + 10);
  428.     if (config.disk_in_df1) {
  429.     len = strlen(config.df1_name);
  430.     if (len > MAX_DRV_NAME)
  431.         len = MAX_DRV_NAME;
  432.     Text(window->RPort, (UBYTE *) config.df1_name, len);
  433.     } else {
  434.     Text(window->RPort, (UBYTE *) empty, MAX_DRV_NAME);
  435.     }
  436.     Move(window->RPort, 59, offy + 45 + 10);
  437.     if (config.disk_in_df2) {
  438.     len = strlen(config.df2_name);
  439.     if (len > MAX_DRV_NAME)
  440.         len = MAX_DRV_NAME;
  441.     Text(window->RPort, (UBYTE *) config.df2_name, len);
  442.     } else {
  443.     Text(window->RPort, (UBYTE *) empty, MAX_DRV_NAME);
  444.     }
  445.     Move(window->RPort, 59, offy + 62 + 10);
  446.     if (config.disk_in_df3) {
  447.     len = strlen(config.df3_name);
  448.     if (len > MAX_DRV_NAME)
  449.         len = MAX_DRV_NAME;
  450.     Text(window->RPort, (UBYTE *) config.df3_name, len);
  451.     } else {
  452.     Text(window->RPort, (UBYTE *) empty, MAX_DRV_NAME);
  453.     }
  454. }
  455.  
  456. BOOL
  457. get_string(UBYTE * buff, LONG max_len)
  458. {
  459.     struct Gadget *g, *sg;
  460.     struct NewGadget ng;
  461.     struct Gadget *gl;
  462.     struct Window *win;
  463.     struct IntuiMessage *msg;
  464.     struct StringInfo *strinfo;
  465.     UWORD koodi, msgID;
  466.     ULONG classi;
  467.     APTR address;
  468.     UWORD offy;
  469.     BOOL quit, ok;
  470.  
  471.     g = CreateContext(&gl);
  472.     if (!g)
  473.     return FALSE;
  474.  
  475.     offy = scr->WBorTop + scr->RastPort.TxHeight + 1;
  476.  
  477.     ng.ng_TextAttr = &topaz8;
  478.     ng.ng_VisualInfo = VisInfo;
  479.     ng.ng_Flags = PLACETEXT_IN;
  480.     ng.ng_LeftEdge = 14;
  481.     ng.ng_TopEdge = offy + 34;
  482.     ng.ng_Width = 90;
  483.     ng.ng_Height = 15;
  484.     ng.ng_GadgetText = (UBYTE *) "OK";
  485.     ng.ng_GadgetID = GAD_OK;
  486.     g = CreateGadget(BUTTON_KIND, g, &ng, TAG_DONE);
  487.  
  488.     ng.ng_LeftEdge = 246;
  489.     ng.ng_TopEdge = offy + 34;
  490.     ng.ng_GadgetText = (UBYTE *) "Cancel";
  491.     ng.ng_GadgetID = GAD_CANCEL;
  492.     g = CreateGadget(BUTTON_KIND, g, &ng, TAG_DONE);
  493.  
  494.     ng.ng_LeftEdge = 14;
  495.     ng.ng_TopEdge = offy + 10;
  496.     ng.ng_Width = 322;
  497.     ng.ng_Height = 15;
  498.     ng.ng_GadgetText = NULL;
  499.     ng.ng_GadgetID = GAD_STRING;
  500.     sg = CreateGadget(STRING_KIND, g, &ng,
  501.               GTST_String, buff,
  502.               GTST_MaxChars, max_len,
  503.               TAG_DONE);
  504.     strinfo = (struct StringInfo *) sg->SpecialInfo;
  505.     ok = FALSE;
  506.  
  507.     newwindow.Title = "Amiga Disk File";
  508.     newwindow.Width = 350;
  509.     newwindow.Height = 65;
  510.     newwindow.FirstGadget = gl;
  511.     newwindow.Screen = scr;
  512.  
  513.     newwindow.LeftEdge = scr->MouseX - (newwindow.Width / 2);
  514.     if (newwindow.LeftEdge + newwindow.Width > scr->Width)
  515.     newwindow.LeftEdge = scr->Width - newwindow.Width;
  516.  
  517.     newwindow.TopEdge = scr->MouseY - (newwindow.Height / 2);
  518.     if (newwindow.TopEdge + newwindow.Height > scr->Height)
  519.     newwindow.TopEdge = scr->Height - newwindow.Height;
  520.  
  521.     win = OpenWindow(&newwindow);
  522.     if (win) {
  523.     ActivateGadget(sg, win, NULL);
  524.     quit = FALSE;
  525.     while (!quit) {
  526.         WaitPort(win->UserPort);
  527.         while (msg = (struct IntuiMessage *) GT_GetIMsg(win->UserPort)) {
  528.         classi = msg->Class;
  529.         koodi = msg->Code;
  530.         address = msg->IAddress;
  531.         if (classi == IDCMP_GADGETUP)
  532.             msgID = ((struct Gadget *) msg->IAddress)->GadgetID;
  533.         else
  534.             msgID = msg->Code;
  535.  
  536.         GT_ReplyIMsg((struct IntuiMessage *) msg);
  537.         switch (classi) {
  538.         case IDCMP_CLOSEWINDOW:
  539.             quit = TRUE;
  540.             break;
  541.         case IDCMP_GADGETUP:
  542.             switch (msgID) {
  543.             case GAD_CANCEL:
  544.             quit = TRUE;
  545.             break;
  546.             case GAD_OK:
  547.             case GAD_STRING:
  548.             strcpy(buff, strinfo->Buffer);
  549.             quit = ok = TRUE;
  550.             break;
  551.             }
  552.             break;
  553.         default:
  554.             break;
  555.         }
  556.         }
  557.     }
  558.     CloseWindow(win);
  559.     }
  560.     FreeGadgets(gl);
  561.  
  562.     return ok;
  563. }
  564.